﻿using System;
using System.Collections.Generic;

using System.IO;
using System.Xml;



namespace DailyFx.Book.Repo
{

	public partial class BookImpl : IBook
	{

		/// <summary>
		///		Contains the root folder of this book .
		/// </summary>
		private string _root;

		/// <summary>
		///		Contains the label of this book .
		/// </summary>
		private string _label;

		/// <summary>
		///		c'tor .
		/// </summary>
		public BookImpl( )
		{

		}

		/// <summary>
		///		Gets the root path of the book .
		/// </summary>
		public String Root 
		{
			get { return this._root; }
		}

		/// <summary>
		///		Gets the label of this book .
		/// </summary>
		public String Label
		{
			get { return this._label; }
		}

		/// <summary>
		/// 
		/// </summary>
		public void createBook( string path, string label, bool overwriteIfExsits)
		{ 
			string root = System.IO.Path.Combine( path, label);
			
			if ( System.IO.Directory.Exists( root) )
			{
				if ( overwriteIfExsits )
				{
					System.IO.Directory.Delete( root, true );
				}
			}

			System.IO.Directory.CreateDirectory( root );
		}

		/// <summary>
		/// 
		/// </summary>
		public bool openBook ( string path, string label )
		{
			this._root = path;
			this._label = label;


			string pathImpl = Path.Combine( path, label);
			if ( !Directory.Exists( pathImpl) )
			{
				this._root = null;
				return false;
			}
			else
			{ 
				// parse the symbol & table matrics lookup . 
				return parseBookInfo ();

				// todo: check table(s) ??.
			}

		}

		/// <summary>
		/// 
		/// </summary>
		public void closeBook( bool applyChanges )
		{
			if ( applyChanges )
			{
				// 
			}
			this._root = null;
		}

		/// <summary>
		///		Checks if the table of given label already exists .
		/// </summary>
		public bool hasTable ( string label )
		{
			return this._metrics.Exists ( x => x.Table == label );
		}

		/// <summary>
		///		Gets the table of specified label .
		/// </summary>
		public ITable getTable ( string label )
		{
			throw new NotImplementedException("");

		}

		/// <summary>
		///		Creates a new table of specified label 
		///		and copying the schema structure and records' data from memory pool . 
		/// </summary>
		public ITable createTable ( string label, Schema schema, bool overwriteIfExists )
		{
			throw new NotImplementedException("");
		}

		/// <summary>
		///		Deletes the table of specified label. 
		///		Operation gonna be failed If the table does not exist or is in use (opened) . 
		/// </summary>
		public bool deleteTable ( string label )
		{
			throw new NotImplementedException("");
		}

		/// <summary>
		///		Creates new table "target" by copying both structure and data from the table "source".
		/// </summary>
		public ITable copyTable ( string target, string source, bool overwriteIfExists )
		{
			throw new NotImplementedException("");
		}


		/// <summary>
		///		Creates a schema, which'll be used for creating new tables in the book. 
		/// </summary>
		public bool createSchema ( string alias, Schema schema )
		{
			throw new NotImplementedException( "");
		}

		/// <summary>
		///		Deletes the schema of specified alias .
		/// </summary>
		public bool deleteSchema ( string alias )
		{
			throw new NotImplementedException("");
		}

		/// <summary>
		///		Gets the schema by specified alias .
		/// </summary>
		public Schema getSchema( string alias )
		{
			int index = this._schemas.FindIndex ( x => x.Alias == alias );
			if ( index == -1)
				return null;

			return this._schemas[ index];
		}


		/// <summary>
		///		Adds a new symbol of security to database .
		/// </summary>
		public void addSymbol ( Symbol symbol )
		{
			throw new NotImplementedException("");
		}

		/// <summary>
		///		Deletes the symbol from database .
		/// </summary>
		public bool deleteSymbol(string code)
		{
			throw new NotImplementedException("");
		}

		/// <summary>
		///		Gets the symbol of specified code( identifier ) .
		/// </summary>
		public Symbol getSymbol ( string code )
		{
			int index = this._symbols.FindIndex (x => x.Code == code );
			if ( index == -1 )
				return null;

			return this._symbols[ index];
		}

		/// <summary>
		/// 
		/// </summary>
		public void Dispose ()
		{
			// todo: 
		}
	}
}
